<?php

session_start();
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/config/mblog_config.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/core/mblog/weibooauth.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/love/inc.php");
error_reporting(E_ERROR | E_PARSE);

//error_reporting(E_ALL);
//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	cgi::both($g_cgival["id"], "id", "");
	$g_cgival["id"] = intval($g_cgival["id"]);
	if(!$g_cgival["id"])
	{
		throw new Exception("id呢?");
	}

	cgi::both($g_cgival["need_type"], "need_type", "");
	$g_cgival["need_type"] = intval($g_cgival["need_type"]);
	
	cgi::both($g_cgival["accept"], "accept", "");
	$g_cgival["accept"] = intval($g_cgival["accept"]);
	
	cgi::both($g_cgival["need_num"], "need_num", "");
	$g_cgival["need_num"] = intval($g_cgival["need_num"]);

}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;

	if(!$_SESSION['last_key']['oauth_token'])
	{
		header("location:/sina_api/?url=/love/index.php");
		exit;
	}
	$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
	$current_user_info  = $c->verify_credentials(); // done
	$current_uid = $current_user_info["id"];
	$current_nick = $current_user_info["name"];
	$id = $g_cgival["id"] ;
	$db_love_request = new db_love_request();
	$request_info = $db_love_request->get_item_by_id($id);
	$answer_uid = $request_info["answer_uid"];
	$request_uid = $request_info["request_uid"];
	$request_name = $request_info["request_name"];
	$answer_name = $request_info["answer_name"];
	
	if($current_uid != $answer_uid)
	{
		throw new Exception("不许偷看别人信息！");
	}
	
	$accept = $g_cgival["accept"];
	$need_type = $g_cgival["need_type"];
	$need_num=  $g_cgival["need_num"];
	
	$text ="";
	if($accept == 1)
	{
		$have_get = 1;
		$db_love_request = new db_love_request();
		$res = $db_love_request->insert_love_request($request_uid,$answer_uid,$request_name,$answer_name);
		if(!$res)
		{
			throw new Exception("sorry 数据库出错了");
		}
	
		$db_sina_users = new db_sina_users();
		$request_token_info = $db_sina_users->get_sina_user_info_item_by_sina_uid($request_uid);
		$request_oauth_token = $request_token_info["access_token"];
		$request_oauth_token_secret = $request_token_info["token_secret"];
		$request_token = new WeiboClient( WB_AKEY , WB_SKEY , $request_oauth_token , $request_oauth_token_secret );
		
		$link ="http:\\bless.php$res";
		$text = "@$answer_name 接受 @$request_name 的求婚啦，来祝福吧。11。$link";
		$send_res = $request_token->update($text);
		
		if($send_res["error_code"])
		{
			throw new Exception($send_res["error"]);
		}
		//当前用户发微博 end
		
		$open_id = $send_res["id"];
		$mid = $send_res["mid"];
		$res1 = $db_love_request->update_love_request_by_id($res,$open_id,$mid,$text,$have_get);
		if(!$res1)
		{
			throw new Exception("sorry 数据库更新出错了");
		}
			
		
	
		
	}
	else
	{
		$text = "@$request_name 你被 @$current_nick 拒绝了。。";
		$res = $db_love_request->del_love_request_by_id($id);
		
	}
	
	//admin用户发微博
	$db_sina_users = new db_sina_users();
	$admin_uid = ADMIN_SEND_UID;
	$admin_info = $db_sina_users->get_sina_user_info_item_by_sina_uid($admin_uid);
	$admin_oauth_token = $admin_info["access_token"];
	$admin_oauth_token_secret = $admin_info["token_secret"];
	$admin_token = new WeiboClient( WB_AKEY , WB_SKEY , $admin_oauth_token , $admin_oauth_token_secret );
	$link ="http:\\$res";
	$send_res = $admin_token->update($text);
	//admin用户发微博 end
	
	
	
}
try {
	
	check_cgi_pro();
	get_data();
	$g_show["error"] = 0;
	$g_show["errmsg"] = "";
	
	
} 
catch (Exception $e) 
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
	
}
show_pro();

exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	echo json_encode($g_show);

	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
}



?>